package cn.shop.gameapi.config;

import cn.shop.gameapi.listener.OrderMessageListener;
import cn.shop.gameapi.listener.PartitionDeleteMessageListener;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author : 李一涵
 * @date : Created in 2022/5/28 10:38
 * @description :
 */
@Configuration
@ConditionalOnExpression("'${spring.profiles.active:dev}' == 'online'")
public class ConsumerUtil {

    @Resource
    private AliyunMQConfig mqConfig;

    @Resource(name = "orderTopicProperties")
    private AliyunMQConfig.TopicProperties orderTopicProperties;

    @Resource(name = "removePartitionTopicProperties")
    private AliyunMQConfig.TopicProperties removePartitionTopicProperties;


    @Resource
    private OrderMessageListener orderMessageListener;
    @Resource
    private PartitionDeleteMessageListener partitionDeleteMessageListener;

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean orderConsumer() {
        ConsumerBean consumerBean = new ConsumerBean();
        //配置文件
        Properties properties = mqConfig.getMqProperties();
        //消费者
        properties.setProperty(PropertyKeyConst.GROUP_ID, orderTopicProperties.getGroupId());
        //设置消费者线程数为20个（默认20）
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, "10");
        properties.setProperty(PropertyKeyConst.MaxReconsumeTimes, "5");
        consumerBean.setProperties(properties);
        //订阅消息
        Map<Subscription, MessageListener> subscriptionTable = new HashMap<>();
        //订阅短信消息
        Subscription orderSubscription = new Subscription();
        orderSubscription.setTopic(orderTopicProperties.getTopic());
        orderSubscription.setExpression(orderTopicProperties.getTag());
        subscriptionTable.put(orderSubscription, orderMessageListener);
        consumerBean.setSubscriptionTable(subscriptionTable);
        return consumerBean;
    }

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean partitionDeletedConsumer() {
        ConsumerBean consumerBean = new ConsumerBean();
        //配置文件
        Properties properties = mqConfig.getMqProperties();
        //消费者
        properties.setProperty(PropertyKeyConst.GROUP_ID, removePartitionTopicProperties.getGroupId());
        //设置消费者线程数为20个（默认20）
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, "50");
        properties.setProperty(PropertyKeyConst.MaxReconsumeTimes, "5");
        consumerBean.setProperties(properties);
        //订阅消息
        Map<Subscription, MessageListener> subscriptionTable = new HashMap<>();
        //订阅短信消息
        Subscription orderSubscription = new Subscription();
        orderSubscription.setTopic(removePartitionTopicProperties.getTopic());
        orderSubscription.setExpression(removePartitionTopicProperties.getTag());
        subscriptionTable.put(orderSubscription, partitionDeleteMessageListener);
        consumerBean.setSubscriptionTable(subscriptionTable);
        return consumerBean;
    }
}
